package com.songcha.module_home.ui.fragment.cate

import android.os.Bundle
import android.widget.ImageView
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.alibaba.android.arouter.launcher.ARouter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.scwang.smart.refresh.layout.SmartRefreshLayout
import com.songcha.library_base.mvvm.base.BaseRefreshLoadMoreMvvmFragment
import com.songcha.library_business.bean.book.BookNetBean
import com.songcha.library_business.interfaces.IOnBookClickListener
import com.songcha.library_business.ui.adapter.BookListAdapter
import com.songcha.library_business.util.BusinessJumpUtil
import com.songcha.module_home.R
import com.songcha.module_home.databinding.HomeFragmentHomeCateBinding

class HomeCateFragment : BaseRefreshLoadMoreMvvmFragment<HomeCateViewModel,HomeFragmentHomeCateBinding>() ,IOnBookClickListener{
    private var mAdapter: BookListAdapter?=null

    override fun getLayoutId(): Int {
        return R.layout.home_fragment_home_cate
    }

    override fun initView(savedInstanceState: Bundle?) {
        super.initView(savedInstanceState)

        arguments?.let {
            viewModel.cateId= it.getInt("cate_id")
        }

       /* viewModel.bookList.observe(this){data->
            initRcv(data)
        }
*/
    }

    override fun onGetData() {
        viewModel.getHomeCateBookList()
    }

    override fun onGetLoadMoreData() {
        viewModel.getHomeCateBookList()
    }

    //TODO
    override fun initLoadMoreListData() {

    }

    override fun initListData() {
        databinding.homeCateRcv.apply {
            layoutManager=LinearLayoutManager(context)
            mAdapter=BookListAdapter(viewModel.rlst.value!!,this@HomeCateFragment)
            adapter=mAdapter
        }
    }

    /*private fun initRcv(list:MutableList<BookNetBean>){
        databinding.homeCateRcv.apply {
            layoutManager=LinearLayoutManager(context)
            mAdapter=BookListAdapter(list,this@HomeCateFragment)
            adapter=mAdapter
        }
    }*/

    override fun getRefreshLayout(): SmartRefreshLayout {
        return databinding.homeCateSrl
    }

    override fun getListAdapter(): RecyclerView.Adapter<BaseViewHolder>? {
        if(mAdapter==null) return null
        return mAdapter as RecyclerView.Adapter<BaseViewHolder>
    }

    override fun onBookClick(bookId: String,img: ImageView) {
        BusinessJumpUtil.toBookDetail(bookId)
    }


}